﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class PhieuDieuTraCoSoService : IService<PhieuDieuTraCoSo>
    {
        private readonly IEntityRepository<PhieuDieuTraCoSo> _phieuDieuTraCoSoRepository;
        public PhieuDieuTraCoSoService(IEntityRepository<PhieuDieuTraCoSo> phieuDieuTraCoSoRepository)
        {
            _phieuDieuTraCoSoRepository = phieuDieuTraCoSoRepository;
        }

        public PhieuDieuTraCoSo Create(PhieuDieuTraCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _phieuDieuTraCoSoRepository.InsertOnCommit(t);
                _phieuDieuTraCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public PhieuDieuTraCoSo Update(PhieuDieuTraCoSo t)
        {
            PhieuDieuTraCoSo item;
            try
            {
                item = _phieuDieuTraCoSoRepository.Get(t.Id);
                item.TenCoSo = t.TenCoSo;
                item.Email = t.Email;
                item.Website = t.Website;
                item.NamThanhLap = t.NamThanhLap;
                item.DiaChi_So = t.DiaChi_So;
                item.DiaChi_Duong = t.DiaChi_Duong;
                item.DiaChi_Phuong = t.DiaChi_Phuong;
                item.DiaChi_Quan = t.DiaChi_Quan;
                item.Khu = t.Khu;
                item.NguoiDungDauCoSo = t.NguoiDungDauCoSo;
                item.DienThoai = t.DienThoai;
                item.NgayLapPhieu = t.NgayLapPhieu;
                item.CanBoQuanLyId = t.CanBoQuanLyId;
                item.CapQuanLyHanhChinhId = t.CapQuanLyHanhChinhId;
                item.HinhThucDauTuId = t.HinhThucDauTuId;
                item.ThanhPhanKinhTeId = t.ThanhPhanKinhTeId;
                item.NganhId = t.NganhId;
                item.LoaiHinhHoatDongId = t.LoaiHinhHoatDongId;
                item.CoQuanQuanLyPCCCId = t.CoQuanQuanLyPCCCId;
                item.LoaiCoSoId = t.LoaiCoSoId;
                item.Status = t.Status;
                _phieuDieuTraCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public PhieuDieuTraCoSo Detail(Guid id)
        {
            var item = _phieuDieuTraCoSoRepository.Get(id);
            return item;
        }

        public List<PhieuDieuTraCoSo> ToList()
        {
            var item = _phieuDieuTraCoSoRepository.GetAll().ToList();
            return item;
        }

        public void Delete(PhieuDieuTraCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _phieuDieuTraCoSoRepository.DeleteOnCommit(t);
                _phieuDieuTraCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
